<?php
/**
 * author:歪妻
 * Date: 2020/11/2 0002
 */

require_once 'lib/common.php';
require_once 'lib/view-post.php';
session_start();
$postId = postId();
$pdo = getPDO();
$row = getView($pdo,$postId);
$commentCount = $row['comment_count'];
//如果帖子不存在，我们在这里处理
if (!$row)
{
    redirectAndExit('index.php?not-found=1');
}

$errors = null;
if ($_POST)
{
    switch ($_GET['action'])
    {
        case 'add-comment':
            $commentData = array(
                'name' => $_POST['comment-name'],
                'website' => $_POST['comment-website'],
                'text' => $_POST['comment-text'],
            );
            $errors = handleAddComment($pdo, $postId, $commentData);
            break;
        case 'delete-comment':
            $deleteResponse = $_POST['delete-comment'];
            handleDeleteComment($pdo, $postId, $deleteResponse);
            break;
    }
}

?>

<!DOCTYPE html>
<html>
<head>
    <title>
        A blog application |
        <?php echo htmlEscape($row['title']) ?>
    </title>
    <?php require "templates/head.php"?>
</head>
<body>
<?php require 'templates/title.php' ?>
<div class="post">
    <h2>
        <?php echo htmlEscape($row['title']) ?>
    </h2>

    <div class="date">
        <?php echo convertSqlDate($row['created_at']) ?>
    </div>
        <?php // 这已经转义了，所以不需要进一步转义 ?>
        <?php echo convertNewlinesToParagraphs($row['body']) ?>
</div>
<?php require 'templates/list-comments.php'?>
<?php // 我们在此HTML片段中使用$ commentData ?>
<?php require 'templates/comments-form.php' ?>
</body>
</html>